package com.shen;

import com.shen.pojo.MobileCard;
import com.shen.server.NetPackage;
import com.shen.server.ServicePackage;
import com.shen.server.SuperPackage;
import com.shen.server.TalkPackage;
import com.shen.util.CardUtil;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Author:shen
 * CreateDate:2020/6/11.
 * Function:
 */
public class SoSoMgr {
    static Scanner scanner=new Scanner(System.in);
    static CardUtil util=new CardUtil();
    public static void main(String[] args) {
            start();
    }
    /**
     * 判断充值金额
     */
    public static double checkGetMoney(double money){
        if(money<50){
            System.out.print("请输入充值金额：");
             money=scanner.nextDouble();
           return checkGetMoney(money);
        }else{
            return money;
        }
    }
    /**
     * 话费充值
     */
    public static void cardCharge(){
        System.out.print("请输入充值卡号：");
        String card=scanner.next();
        MobileCard mobileCard = util.selectMobileCard(card);
        if(mobileCard!=null){
            System.out.print("请输入充值金额：");
            double money = checkGetMoney(scanner.nextDouble());
            util.chargeMoney(mobileCard,money);
            System.out.println("充值成功！当前话费余额为："+mobileCard.getMoney());
        }else{
            System.out.println("该卡号不存在！");
        }
    }
    /*
        使用嗖嗖
     */
    public static void useSoSo(){
        System.out.print("请输入手机卡号：");
        String card=scanner.next();
        MobileCard mobileCard = util.selectMobileCard(card);
        if(mobileCard!=null){
          util.utilSoso(mobileCard);
        }else{
            System.out.println("该卡号不存在！");
        }
    }
    /**
     *查看资费说明
     */
    public static void showDiscri(){
        System.out.println("请输入卡号：");
        String card=scanner.next();
        MobileCard mobileCard = util.selectMobileCard(card);
        if(mobileCard!=null){
            util.showDescription(mobileCard);
        }else{
            System.out.println("该卡号不存在！");
        }
    }
    public static void start(){
        boolean isflag=false;
        do{
            System.out.println("*******************欢迎使用搜搜移动业务大厅********************");
            System.out.println("1.用户登录\t2.用户注册\t3.使用嗖嗖\t4.话费充值\t5.资费说明\t6.退出系统");
            System.out.print("请选择：");
            int menu=scanner.nextInt();
            switch (menu){
                case 1:
                    MobileCard mobileCard = checkUserPhoneAndPwd();
                    if(mobileCard!=null){
                         cardMenu(mobileCard.getCardNumber());
                    }
                    break;
                case 2:
                    registCard();
                    break;
                case 3:
                    useSoSo();
                    break;
                case 4:
                    cardCharge();
                    break;
                case 5:
                    showDiscri();
                    break;
                case 6:
                    System.out.println("感谢使用！");
                    isflag=true;
                    break;
            }
        }while (isflag==false);
    }



    /**
     * 用户验证
     * @return
     */
    public static MobileCard checkUserPhoneAndPwd(){
        System.out.print("请输入手机号：");
        String phoneNumber=scanner.next();
        System.out.print("请输入密码：");
        String password=scanner.next();
        MobileCard existCard = util.isExistCard(phoneNumber, password);
        if(existCard!=null){
            return existCard;
        }else{
            System.out.println("用户名或密码错误！");
            return null;
        }
    }

    /**
     * 用户注册新卡
     */
   public static void registCard(){
       //创建一个用户
       MobileCard mobileCard=new MobileCard();
       //接收卡号
       String[] strings=new String[9];
       System.out.println("************可选卡号************");
       for (int i = 0; i <strings.length ; i++) {
           strings[i]=util.createNumber();
           System.out.print((i+1)+":"+strings[i]+"\t");
           if((i+1)%3==0){
               System.out.print("\n");
           }
       }
       //检查输入的卡号
       int cardNumber = choseCard(1,9,"卡号");
       mobileCard.setCardNumber(strings[cardNumber-1]);
       //检查输入的套餐
       System.out.println("1.话痨套餐\t2.网虫套餐\t3.超人套餐");
       int menu = choseCard(1, 3, "套餐");
       ServicePackage servicePackage = choiceService(menu);
       mobileCard.setServicePackage(servicePackage);
       //姓名
       System.out.print("请输入姓名：");
       String name=scanner.next();
       mobileCard.setUserName(name);
       //密码
       System.out.print("请输入密码：");
       String pwd=scanner.next();
       mobileCard.setPassWord(pwd);
       //话费检查
       System.out.print("请预存话费：");
       double money=isCanPayMenu(servicePackage,scanner.nextDouble());
       //余额
       mobileCard.setMoney(money-servicePackage.price);
       //消费
       mobileCard.setConsumAmount(mobileCard.getConsumAmount()+money);
       //增加用户
       MobileCard userMobileCard = util.addCard(mobileCard);
       if(userMobileCard!=null){
           System.out.println("注册成功！卡号："+userMobileCard.getCardNumber()+" 用户名："+mobileCard.getUserName()+" 当前余额："+mobileCard.getMoney());
           System.out.println(userMobileCard.getServicePackage().toString());
       }else{
           System.out.println("注册失败！");
       }
   }
    /**
     * 检查能否支付套餐费用
     */
    public static double isCanPayMenu(ServicePackage servicePackage,double money ){
        if(money<servicePackage.price){
            System.out.print("你预存的话费不足以支付本月固定套餐资费，请重新充值：");
            money=scanner.nextDouble();
            return isCanPayMenu(servicePackage,money);
        }else{
            return money;
        }
    }
    /**
     * 套餐
     */
    public static ServicePackage choiceService(int menu){
        ServicePackage servicePackage=null;
        switch (menu){
            case 1:
                servicePackage=new TalkPackage();
                break;
            case 2:
                servicePackage=new NetPackage();
                break;
            case 3:
                servicePackage=new SuperPackage();
                break;
        }
        return servicePackage;
    }
    /**
     *
     * @return
     */
    public static int choseCard(int min,int max,String founction){
        System.out.print("请选择"+founction+"：(请输入"+min+"-"+max+"的序号)");
        int number=scanner.nextInt();
        if(number<min||number>max){
           return choseCard(min,max,founction);
        }else{
            return number;
        }
    }

    /**
     * 套餐变更
     */
    public static void changeService(String mobileCard){
        MobileCard card = util.selectMobileCard(mobileCard);
        System.out.println("1.话痨套餐\t2.网虫套餐\t3.超人套餐");
        int menu = choseCard(1, 3, "套餐");
        util.changingPack(card,menu);
    }


    public static void cardMenu(String card) {
        boolean isReturn=false;
        do {
            System.out.println("*******************嗖嗖移动用户菜单********************");
            System.out.println("1.用户账单查询\n2.套餐余量查询\n3.打印消费详单\n4.套餐变更\n5.办理退网");
            System.out.println("请选择(输入1-5选择功能，其他键返回上一级)");
            int num=scanner.nextInt();
            switch (num){
                case 1:
                    util.showAmountDetail(card);
                    break;
                case 2:
                    util.showRemainDetail(card);
                    break;
                case 3:
                    util.printConsumInfo(card);
                    break;
                case 4:
                    changeService(card);
                    break;
                case 5:
                    MobileCard delCard = util.delCard(card);
                    System.out.println("卡号"+delCard.getCardNumber()+"办理退网成功！\n感谢使用");
                    break;
                default:
                    isReturn=true;
                    break;
            }
        }while (isReturn==false);

    }



}
